package com.wyf.reggie.Service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyf.reggie.Entity.Category;
import com.wyf.reggie.Entity.Dish;
import com.wyf.reggie.Entity.Setmeal;
import com.wyf.reggie.ExceptinonHandler.CustomException;
import com.wyf.reggie.Mapper.CategoryMapper;
import com.wyf.reggie.Service.CategoryService;
import com.wyf.reggie.Service.DishService;
import com.wyf.reggie.Service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImp extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(queryWrapper);
        if(count>0) {
            throw new CustomException("当前分类项关联了菜品,不能删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(queryWrapper1);
        if(count1>0){
            throw new CustomException("当前分类项关联了套餐,不能删除");
        }
        super.removeById(id);
    }
}
